//
//  MyStatusBarController.m
//  Ghac 4S
//
//  Created by apple on 11-10-10.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyStatusBarController.h"
@interface MyStatusBarController(privete)
-(void)setBatteryState;
-(void)setTimeState;
@end




@implementation MyStatusBarController
@synthesize batteryImageView;
@synthesize batteryImageRed;
@synthesize timeLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setBatteryState];
    [self setTimeState];
    // Do any additional setup after loading the view from its nib.
//    NSTimeInterval internal = nsinter
//    NSTimer* time = [[NSTimer alloc] initWithFireDate:Nil interval:1.0 target:self selector:@selector(timefun:) userInfo:Nil repeats:YES];
    NSTimer* time = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timefun:) userInfo:Nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:time forMode:NSRunLoopCommonModes];

}
- (void)timefun:(id) sender
{
    [self setBatteryState];
    [self setTimeState];
}
-(void)setBatteryState
{
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    CGFloat level = [[UIDevice currentDevice] batteryLevel];
    level = level < 0 ? 0 : level;
    CGRect batteryFrame = self.batteryImageView.frame;
    CGRect frame = CGRectMake(batteryFrame.origin.x, batteryFrame.origin.y, (batteryFrame.size.width - 1) * level, batteryFrame.size.height);
    [self.batteryImageRed setFrame:frame];
//    NSLog(@"%@, %f", [NSValue valueWithCGRect:frame], level);
}
-(void)setTimeState
{
    NSDate* date = [[NSDate alloc] init];
    NSDateFormatter* dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:@"hh:mm a"];
    self.timeLabel.text = [dateformater stringFromDate:date];
    [date release];
    [dateformater release];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    self.batteryImageRed = nil;
    self.timeLabel = nil;
    self.batteryImageView = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
-(void)dealloc
{
    [self viewDidUnload];
    [super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
